package com.paomi.client.activity.takeTicket;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.takeTicket.adapter.TakeListAdapter;
import com.paomi.client.activity.takeTicket.model.TakeTicketBean;
import com.paomi.client.activity.takeTicket.model.TicketPersonBean;
import com.paomi.client.base.BaseMvpActivity;
import com.paomi.client.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class TakeTicketListActivity extends BaseMvpActivity<TakeTicketListView, TakeTicketListPresenter> implements TakeTicketListView {

    @BindView(R.id.recycler_list)
    RecyclerView recyclerList;
    @BindView(R.id.add_take_btn)
    Button addTakeBtn;
    private TakeListAdapter takeListAdapter;
    private List<TakeTicketBean> ticketBeans=new ArrayList<>();
    private int position=0;
    private int pageNum=1;

    @NonNull
    @Override
    public TakeTicketListPresenter createPresenter() {
        return new TakeTicketListPresenter();
    }

    @Override
    protected String setTitle() {
        return getResources().getString(R.string.take_message);
    }

    @Override
    protected void showBack(TextView button_goback) {
        button_goback.setVisibility(View.VISIBLE);
        button_goback.setTypeface(iconfont);
        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + getResources().getString(R.string.back));
    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_take_ticket_list;
    }

    @Override
    protected void initEvent() {
        recyclerList.setLayoutManager(UIUtils.getRecyclerViewManager(false,this));
        takeListAdapter=new TakeListAdapter(this,iconfont);
        recyclerList.setAdapter(takeListAdapter);
        getPresenter().getTicketPersonList(pageNum);
        takeListAdapter.OnItemClick(new TakeListAdapter.OnItemClick() {
            @Override
            public void setOnItemClick(View view, int pos) {
                position=pos;
                TakeTicketBean takeTicketBean=ticketBeans.get(pos);
                startActivityForResult(TakeTicketActivity.class,takeTicketBean,9002);
            }

            @Override
            public void setOnCheckClick(View view, int pos) {
                TakeTicketBean takeTicketBean=ticketBeans.get(pos);
                Intent intent=new Intent();
                intent.putExtra("takeBean",takeTicketBean);
                setResult(2001,intent);
                finish();
            }
        });
    }

    @OnClick(R.id.add_take_btn)
    public void onViewClicked() {
        startActivityForResult(TakeTicketActivity.class,null,9001);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==9001){//添加
            if(resultCode==2001 && data!=null){
                pageNum=1;
                getPresenter().getTicketPersonList(pageNum);
//                TakeTicketBean ticketBean=(TakeTicketBean) data.getSerializableExtra("takeBean");
//                if(ticketBean!=null){
//                   ticketBeans.add(ticketBean);
//                   takeListAdapter.setData(ticketBeans);
//                   takeListAdapter.notifyDataSetChanged();
//                }
            }
        }else if(requestCode==9002){//改变
            if(resultCode==2001 && data!=null){
                TakeTicketBean ticketBean=(TakeTicketBean) data.getSerializableExtra("takeBean");
                if(ticketBean!=null){
                    if(ticketBeans.size()>0){
                        for(int i=0;i<ticketBeans.size();i++){
                            if(i == position){
                                TakeTicketBean ticketBean1=ticketBeans.get(i);
                                ticketBean1.setName(ticketBean.getName());
                                ticketBean1.setMobile(ticketBean.getMobile());
                                ticketBean1.setCode(ticketBean.getCode());
                            }
                        }
                        takeListAdapter.setData(ticketBeans);
                        takeListAdapter.notifyDataSetChanged();
                    }
                }else {
                    pageNum=1;
                    getPresenter().getTicketPersonList(pageNum);
                }
            }else if(resultCode==2001){
                pageNum=1;
                getPresenter().getTicketPersonList(pageNum);
            }
        }
    }

    @Override
    public void setPersonList(TicketPersonBean ticketPersonBean) {
        if(ticketPersonBean.getReturnData()!=null){
            if(pageNum==1){
                ticketBeans.clear();
            }
            ticketBeans.addAll(ticketPersonBean.getReturnData().getData());
            takeListAdapter.setData(ticketBeans);
            takeListAdapter.notifyDataSetChanged();
        }
    }
}
